package cn.stylefeng.guns.modular.ht.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.ht.entity.BackRegister;
import cn.stylefeng.guns.modular.ht.mapper.BackRegisterMapper;
import cn.stylefeng.guns.modular.ht.model.params.BackRegisterParam;
import cn.stylefeng.guns.modular.ht.model.result.BackRegisterResult;
import  cn.stylefeng.guns.modular.ht.service.BackRegisterService;
import cn.stylefeng.guns.sys.core.constant.factory.ConstantFactory;
import cn.stylefeng.guns.sys.core.shiro.ShiroKit;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 返回地登记 服务实现类
 * </p>
 *
 * @author 
 * @since 2020-02-01
 */
@Service
public class BackRegisterServiceImpl extends ServiceImpl<BackRegisterMapper, BackRegister> implements BackRegisterService {

    @Override
    public void add(BackRegisterParam param){
        BackRegister entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(BackRegisterParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(BackRegisterParam param){
        BackRegister oldEntity = getOldEntity(param);
        BackRegister newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public BackRegisterResult findBySpec(BackRegisterParam param){
        return null;
    }

    @Override
    public List<BackRegisterResult> findListBySpec(BackRegisterParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(BackRegisterParam param){
        Page pageContext = getPageContext();
        if (ShiroKit.hasAnyRoles("company_admin")) {
            Long deptId = ShiroKit.getUser().getDeptId();
            List<Long> subDeptId = ConstantFactory.me().getSubDeptId(deptId);
            subDeptId.add(deptId);
            param.setDeptIds(subDeptId);
        }
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public LayuiPageInfo findPageBySpecForApi(BackRegisterParam param) {
        Page pageContext = getPageContext();
        Page<BackRegisterResult> backRegisterResultPage = this.baseMapper.customPageListForApi(pageContext, param);
        return LayuiPageFactory.createPageInfo(backRegisterResultPage);
    }

    private Serializable getKey(BackRegisterParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private BackRegister getOldEntity(BackRegisterParam param) {
        return this.getById(getKey(param));
    }

    private BackRegister getEntity(BackRegisterParam param) {
        BackRegister entity = new BackRegister();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
